home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 December / macformat-019.iso / Shareware in MacFormat / Thermometer ƒ / Thermometer Source / thermWindow.c < prev   
Encoding:
C/C++ Source or Header  |  1992-06-26  |  4.1 KB  |  215 lines  |  [TEXT/KAHL]

  1. /*****
  2.  * thermWindow.c
  3.  *
  4.  *        The window routines for the Thermometer™
  5.  *
  6.  *        Copyright © 1991-1992 Bernard Bernstein. All rights reserved.
  7. *****/
  8.  
  9. #include <Color.h>
  10. #include "thermo.h"
  11.  
  12. WindowPtr    tempWindow;
  13. Rect        dragRect;
  14. extern    short temperature;
  15. extern thermPrefsRec thermPrefs;
  16. Rect        thermRect;
  17.  
  18. /****
  19.  * SetUpWindow()
  20.  *
  21.  *    Create the Thermometer window, and open it.
  22.  *
  23.  ****/
  24. SetUpWindow()
  25. {
  26.     dragRect = screenBits.bounds;
  27.     
  28.     tempWindow = GetNewWindow(WINDMain, 0L, -1L);
  29.  
  30.     SetPort(tempWindow);
  31.  
  32.     ComputeThermRect();
  33. }
  34. /* end SetUpWindow */
  35.  
  36. /***
  37.  * UpdateWindowTitle
  38.  *
  39.  *        Set the window title to C or F
  40.  ***/
  41. UpdateWindowTitle()
  42. {
  43.     Str255    winTitle;
  44.     
  45.     if (thermPrefs.scale == farenheit)
  46.         GetIndString(&winTitle, STRScales, STRFarenTitle);
  47.     else
  48.         GetIndString(&winTitle, STRScales, STRCelciTitle);
  49.  
  50.     SetWTitle(tempWindow, winTitle);
  51. }
  52.  
  53.  
  54.  
  55. /***
  56.  * ComputeThermRect
  57.  *
  58.  *        What rect is the mercury in?
  59.  ***/
  60. ComputeThermRect()
  61. {
  62.     GrafPtr    savePort;
  63.     GetPort(&savePort);
  64.     SetPort(tempWindow);
  65.  
  66.     thermRect = tempWindow->portRect;
  67.     thermRect.top += 5;
  68.     thermRect.bottom -= 20;
  69.     thermRect.right -= 5;
  70.     thermRect.left = thermRect.right - 10;
  71.     SetPort(savePort);
  72. }
  73.  
  74. /***
  75.  * DoWindowClick
  76.  *
  77.  *        Window was clicked. Grow the window if in grow box
  78.  ***/
  79. DoWindowClick(Point mousePt)
  80. {
  81.     long         newPt;
  82.     Rect        sizeRect;
  83.     short        w,h;
  84.     Point        thermBR, thermTL;
  85.     Rect        devRect;
  86.     GDHandle    saveDev;
  87.     GDHandle    thisDev;
  88.     
  89.     
  90.     thermBR = botRight(thermRect);
  91.     thermTL = topLeft(thermRect);
  92.     LocalToGlobal(&thermTL);
  93.     LocalToGlobal(&thermBR);
  94.     if (mousePt.v > thermBR.v) {
  95.         sizeRect = tempWindow->portRect;
  96.         sizeRect.left = sizeRect.right += 1;
  97.         sizeRect.top = 50;
  98.         saveDev = GetGDevice();
  99.         thisDev = GetDeviceList();
  100.         while (thisDev != 0L) {
  101.             devRect = (**thisDev).gdRect;
  102.             if (PtInRect(thermTL, &devRect)) {
  103.                 sizeRect.bottom = devRect.bottom - devRect.top;
  104.             }
  105.             thisDev = GetNextDevice(thisDev);
  106.         }
  107.         SetGDevice(saveDev);
  108.         newPt = GrowWindow(tempWindow, mousePt, &sizeRect);
  109.         if (newPt != 0L) {
  110.             h = HiWord(newPt);
  111.             w = LoWord(newPt);
  112.             SizeWindow(tempWindow,w,h,FALSE);
  113.             ComputeThermRect();
  114.         }
  115.         InvalRect(&tempWindow->portRect);
  116.     }
  117. }
  118.  
  119. /*****
  120.  * DrawTempWindow
  121.  *
  122.  *    Draws the temperature window.
  123.  *
  124.  *****/
  125. DrawTempWindow(active)
  126.  
  127.     short    active;
  128.  
  129. {
  130.     GrafPtr    savePort;
  131.     short    i;
  132.     Rect    frame;
  133.     short    height,width;
  134.     short    min, max, range;
  135.     float    interval;
  136.     short    linev;
  137.     Str255    numStr;
  138.     Rect    merc;
  139.     short    adjustedTemp;
  140.     Rect    well;
  141.     
  142.     GetPort(&savePort);
  143.     SetPort(tempWindow);
  144.     
  145.     frame = tempWindow->portRect;
  146.     height = thermRect.bottom - thermRect.top;
  147.     
  148.     EraseRect(&frame);
  149.     
  150.     min = thermPrefs.min;
  151.     max = thermPrefs.max;
  152.     range = max - min;
  153.     interval = (float)height / range;
  154.  
  155.     adjustedTemp = (temperature - min) * interval;
  156.     if (adjustedTemp > height)
  157.         adjustedTemp = height;
  158.     else if (adjustedTemp < 0) 
  159.         adjustedTemp = 0;
  160.     
  161.     FrameRect(&thermRect);
  162.     
  163.     well = thermRect;
  164.     well.right += 4;
  165.     well.left -= 4;
  166.     well.top = well.bottom;
  167.     well.bottom = frame.bottom;
  168.     FrameRoundRect(&well, 8, 8);
  169.     FillRoundRect(&well, 8, 8, dkGray);
  170.     
  171.     merc = thermRect;
  172.     merc.top = merc.bottom - adjustedTemp;
  173.     FillRect(&merc, dkGray);
  174.  
  175.     TextSize(9);
  176.     PenMode(patXor);
  177.     for (i=min; i<max; i++) {
  178.         if (i % 10 == 0) {
  179.             linev = thermRect.bottom - ((i-min) * interval);
  180.             
  181.             MoveTo(frame.left, linev);
  182.             LineTo(thermRect.left - 1, linev);
  183.             MoveTo(thermRect.right, linev);
  184.             LineTo(frame.right, linev);
  185.  
  186.             MoveTo(frame.left + 2, linev);
  187.             NumToString((long)i, &numStr);
  188.             DrawString(numStr);
  189.         } else if (interval > 2) {
  190.             if (i % 2 == 0) {
  191.                 linev = thermRect.bottom - ((i-min) * interval);
  192.                 MoveTo(thermRect.left - 3, linev);
  193.                 LineTo(thermRect.left - 1, linev);
  194.                 MoveTo(thermRect.right, linev);
  195.                 LineTo(thermRect.right + 3, linev);
  196.             }
  197.         } else if (interval > 1) {
  198.             if (i % 5 == 0) {
  199.                 linev = thermRect.bottom - ((i-min) * interval);
  200.                 MoveTo(thermRect.left - 3, linev);
  201.                 LineTo(thermRect.left - 1, linev);
  202.                 MoveTo(thermRect.right, linev);
  203.                 LineTo(thermRect.right + 3, linev);
  204.             }
  205.         }
  206.     }
  207.     
  208.     merc = thermRect;
  209.     merc.top = merc.bottom - (temperature - min) * interval;
  210.     FillRect(&merc, dkGray);
  211.     
  212.     SetPort(savePort);
  213. }
  214. /* end DrawTempWndow */
  215.